package com.jin.gulimall.search.thread;

import java.util.concurrent.*;

public class ThreadTest {
    //线程池一般只设置一两个在整个系统中
    public static ExecutorService executor = Executors.newFixedThreadPool(10);
    //CompletableFuture.runAsync
    //CompletableFuture.supplyAsync
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main...start...");
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getName());
//            int i = 6/0;
//            System.out.println("运行结果：" + i);
//            return i;
////            void accept(T t, U u);
//        }, executor).whenCompleteAsync((res,exception)->{
//            System.out.println("运行完成：结果为"+res+",异常为："+exception);
//        }).exceptionally(throwable ->{
//            //可以感知结果，并返回默认值
//                return 10;
//        });
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getName());
//            int i = 6 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executor).handle((res, exception) -> {
//            if (res != null) {
//                return res * 2;
//            }
//            if (exception != null) {
//                return 0;
//            }
//            return 0;
//        });
//        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getName());
//            int i = 6 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executor).thenApply((res) -> {
//            System.out.println("任务2启动了......" + res);
//            return "hello:" + res;
//        });
//        //R apply(T t, U u);
//        System.out.println("main...end..."+future.get());
//    }
        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getName());
            int i = 6 / 2;
            System.out.println("运行结果：" + i);
            return i;
        }, executor);

        CompletableFuture<Integer> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getName());
            int i = 4;
            try {
                Thread.sleep(3000);
                System.out.println("运行结果：" + i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return i;
        }, executor);

//        future01.thenAcceptBothAsync(future02, (f1, f2) -> {
//            System.out.println("任务1结果："+f1+",:任务2结果"+f2);
//        },executor);
//        CompletableFuture<Integer> future = future01.thenCombineAsync(future02, (f1, f2) -> {
//            System.out.println("任务一结果：" + f1 + ",:任务二结果:" + f2);
//            return 10;
//        }, executor);
//        future01.runAfterEitherAsync(future02, () -> {
//            System.out.println("哈哈哈哈哈哈");
//        },executor);
//        System.out.println("main...end...");
//        CompletableFuture<Void> future = CompletableFuture.allOf(future01, future02);
//        future.get();
        CompletableFuture<Object> future = CompletableFuture.anyOf(future01, future02);
        future.get();
        System.out.println("main...end...");
    }
    public  void thread(String[] args) throws ExecutionException, InterruptedException {
        /**
         * 1.thread,继承Thread类
         * 2.Runnable
         * 3.Callable+FutureTask
         * 4.线程池
         * 区别：
         *   1.2不能得到返回值，3可以获取返回值
         *   1.2.3都不能控制资源
         *   4可以控制资源，性能稳定
         *
         *   线程池工作流程：
         *   1.线程池创建，准备好core数量的核心线程，准备接受任务
         *   2.core满了，就将再进来的任务放入阻塞队列中，空闲的core就会自己去阻塞队列获取任务执行
         *   3.阻塞队列满了，就直接开新线程执行，最大只能开到max指定的数量
         *   4.max都执行完成，有很多空闲，在指定的时间keepAliveTime以后，释放max-core（减）这些线程
         *
         *   面试题：
         *   一个线程池，core:7 max:20 queue:50   100并发进来怎么分配？
         *   7个立即执行，50个进入阻塞队列，再开13个新线程进行执行，剩下30个就使用拒绝策略。
         */
        //原生线程池
        //ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5,20,10,null,null,null,null);
//        Executors.newCachedThreadPool() core为0,所有都可回收
//        Executors.newFixedThreadPool()  core为max,所有都不可回收
//        Executors.newScheduledThreadPool() 定时任务
//        Executors.newSingleThreadExecutor() 单线程,core与max均为1
        System.out.println("main....start...");
        //1.thread
//        Thread01 thread01 = new Thread01();
//        thread01.start();
//        Runnable01 runnable01 = new Runnable01();
//        Thread thread = new Thread(runnable01);
//        thread.start();
//        FutureTask<Integer> task = new FutureTask<>(new Callable01());
//        new Thread(task).start();
//        Integer value = task.get();
        executor.execute(new Runnable01());
        System.out.println("main....end...");

    }
    public static class Thread01 extends Thread{
        @Override
        public void run() {
            System.out.println("当前线程："+Thread.currentThread().getName());
            int i=6;
            System.out.println("运行结果："+i);
        }
    }

    public static class Runnable01 implements Runnable{

        @Override
        public void run() {
            System.out.println("当前线程："+Thread.currentThread().getName());
            int i=6;
            System.out.println("运行结果："+i);
        }
    }

    public static class Callable01 implements java.util.concurrent.Callable<Integer>{

        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程："+Thread.currentThread().getName());
            int i=6;
            System.out.println("运行结果："+i);
            return i;
        }
    }
}
